package eu.itcsolutions.android.cursor;
/* 
 * http://www.edumobile.org/android/android-development/use-of-sqlite/
 * http://android-er.blogspot.com/2011/06/delete-row-in-sqlite-database.html
 * 
 */

/*package net.learn2develop.PurchaseOrders;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.util.Log;

public class DataManipulator1 {
	
        private static final  String DATABASE_NAME = "easysales2.db";
        private static final int DATABASE_VERSION = 1;
        static final String TABLE_COMANDA   = "ordersa3";
        static final String TABLE_ORDERS   = "orders2";
        static final String TABLE_PRODUCTS = "products2";
        static final String TABLE_CLIENTS = "clients2";
        static final String TABLE_SETUP = "setup";
        private static Context context;
        static SQLiteDatabase db;
        List < CommandLine > orderOfClient ;
        private SQLiteStatement insertOrderTemplate;
        private SQLiteStatement insertProductTemplate;
        private SQLiteStatement insertClientTemplate;
        private SQLiteStatement insertSetupTemplate;
        private SQLiteStatement insertOrdersaTemplate;
        
        private static final String INSERT_COMANDA = "insert into " + TABLE_COMANDA + " (nrdoc,data_c,gestiune_id,tert_id,valoare,nrlc_id,data_l,user_id,nivacc,operare,verstor,tiparit,facturat,zscadenta,pr_disc_expl,val_disc_expl,NrFact,data_f) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        private static final String INSERT_ORDERS = "insert into " + TABLE_ORDERS + " (clientName,productName,piecesNumber,discountNumber) values (?,?,?,?)";
    	private static final String INSERT_PRODUCTS = "insert into " + TABLE_PRODUCTS + " (ID, Name, Price, Symbol) values (?,?,?,?)";
    	private static final String INSERT_CLIENTS = "insert into " + TABLE_CLIENTS + " (Agent, Client, Route, Zone) values (?,?,?,?)";
    	private static final String INSERT_SETUP = "insert into " + TABLE_SETUP + " (UtilizatorID, UserName, Parola) values (?,?,?)";
        
   		public DataManipulator(Context context ) {
                DataManipulator.context = context;
                OpenHelper openHelper = new OpenHelper(DataManipulator.context);
                DataManipulator.db = openHelper.getWritableDatabase();
                this.insertOrdersaTemplate = DataManipulator.db.compileStatement(INSERT_COMANDA);
                this.insertOrderTemplate = DataManipulator.db.compileStatement(INSERT_ORDERS);
                this.insertProductTemplate = DataManipulator.db.compileStatement(INSERT_PRODUCTS);
                this.insertClientTemplate = DataManipulator.db.compileStatement(INSERT_CLIENTS);
                this.insertSetupTemplate = DataManipulator.db.compileStatement(INSERT_SETUP);
                orderOfClient = new ArrayList<CommandLine>();
    	}
          
   		
        public long insertIntoComanda(String nrdoc,String data_c,String gestiune_id,String nrlc_id,String tert_id, String valoare,String data_l,String facturat,String user_id,String operare,
        		String verstor,String tiparit,String nivacc,String zscadenta,String pr_disc_expl,String val_disc_expl,String NrFact,String data_f
        		) {
           this.insertOrdersaTemplate.bindString(1, nrdoc);
           this.insertOrdersaTemplate.bindString(2, data_c);
           this.insertOrdersaTemplate.bindString(3, gestiune_id);
           this.insertOrdersaTemplate.bindString(4, tert_id);
           this.insertOrdersaTemplate.bindString(5, valoare);
           this.insertOrdersaTemplate.bindString(6, nrlc_id);
           this.insertOrdersaTemplate.bindString(7, data_l);
           this.insertOrdersaTemplate.bindString(8, user_id);
           this.insertOrdersaTemplate.bindString(9, nivacc);
           this.insertOrdersaTemplate.bindString(10, operare);
           this.insertOrdersaTemplate.bindString(11, verstor);
           this.insertOrdersaTemplate.bindString(12, tiparit);
           this.insertOrdersaTemplate.bindString(13, facturat);
           this.insertOrdersaTemplate.bindString(14, zscadenta);
           this.insertOrdersaTemplate.bindString(15, pr_disc_expl);
           this.insertOrdersaTemplate.bindString(16, val_disc_expl);
           this.insertOrdersaTemplate.bindString(17, NrFact);
           this.insertOrdersaTemplate.bindString(18, data_f);
           
           return this.insertOrdersaTemplate.executeInsert();
  }
        public long insertIntoOrders(String clientName,String productName,String piecesNumber,String discountNumber) {
                this.insertOrderTemplate.bindString(1, clientName);
                this.insertOrderTemplate.bindString(2, productName);
                this.insertOrderTemplate.bindString(3, piecesNumber);
                this.insertOrderTemplate.bindString(4, discountNumber);
                return this.insertOrderTemplate.executeInsert();
        }

        public long insertIntoProducts(String ID,String Name,String Price,String Symbol) {
                this.insertProductTemplate.bindString(1, ID);
                
                this.insertProductTemplate.bindString(2, Name);
                this.insertProductTemplate.bindString(3, Price);
                this.insertProductTemplate.bindString(4, Symbol);
                return this.insertProductTemplate.executeInsert();
        }
        
        
        public long insertIntoClients(String Agent,String Client,String Route,String Zone) {
        		this.insertClientTemplate.bindString(1, Agent);
        		this.insertClientTemplate.bindString(2, Client);
        		this.insertClientTemplate.bindString(3, Route);
        		this.insertClientTemplate.bindString(4, Zone);
        		return this.insertClientTemplate.executeInsert();
        }

        public long insertIntoSetup(String strID, String strUserName, String strPassword) {
    		this.insertSetupTemplate.bindString(1, strID);
    		this.insertSetupTemplate.bindString(2, strUserName);
    		this.insertSetupTemplate.bindString(3, strPassword);
    		// this.insertClientTemplate.bindString(3, Route);
    		// this.insertClientTemplate.bindString(4, Zone);
    		return this.insertSetupTemplate.executeInsert();
    }

        
        public void adaugaLiniePeComanda(String denumire,String bucati,String discount,String prezenta){
        	CommandLine newLine = new CommandLine(denumire, bucati, discount, prezenta);
        	orderOfClient.add(newLine ) ;
        }

        public void listeazaLiniileComenzii(){
        	Iterator i  = orderOfClient.iterator();
        	while (i.hasNext())
        	{
        		CommandLine value= (CommandLine) i.next();
        		System.out.println(value.getprodus()+ "-" +value.getbucati()+ "-" +value.getcost());
        	}
        }
        
        public void insereazaLiniileComenzii(String clientName){
        	String denProd;
            String nrBuc;
            String disc;
            String prez;
        	
        	Iterator i  = orderOfClient.iterator();
        	while (i.hasNext())
        	{
        		CommandLine value= (CommandLine) i.next();
        		denProd =value.getprodus();
        		nrBuc =value.getbucati();
        		disc =value.getcost();
        		prez =value.getprezenta();
        		// this.db.insertIntoOrders(denProd ,nrBuc ,disc ,prez );
        		this.insertIntoOrders(clientName, denProd , nrBuc, disc);
          		// System.out.println(value.getprodus()+ "-" +value.getbucati()+ "-" +value.getcost());
        	}
        }
        
        public void sincroDB(){
                // ToDo ... Next 
        }

        
        public void deleteAllProducts() {
            // db.delete(TABLE_PRODUCTS, null, null);
            try{
                // db = openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE,null);
                db.execSQL("DELETE FROM " + TABLE_PRODUCTS );
                        Log.i("_DataManipulator_","<DELETE FROM>" + TABLE_PRODUCTS + ">\n");
                // db.close();
            }catch(Exception e){
                // Toast.makeText(getApplicationContext(), "Error encountered while deleting.", Toast.LENGTH_LONG);
            }
        }
        
        
        public void deleteAllClients() {
            try{
            	 db.delete(TABLE_CLIENTS, null, null);
            	 // db = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
            	 //db.execSQL("DELETE FROM " + TABLE_CLIENTS );
                 //Log.i("_DataManipulator_","<DELETE FROM>" + TABLE_CLIENTS + ">\n");
            	 // db.close();
            }catch(Exception e){
                	// Toast.makeText(this, "Error encountered while deleting.", Toast.LENGTH_LONG);
            		// System.out.println(e.getMessage());
            }
        }

        public void deleteAllSetup() {
            try{
            	 db.delete(TABLE_SETUP, null, null);
            }catch(Exception e){
                	// Toast.makeText(this, "Error encountered while deleting.", Toast.LENGTH_LONG);
            		System.out.println(e.getMessage());
            }
        }

        
        public void deleteAll() {
                db.delete(TABLE_ORDERS, null, null);
        }

        

        
        
        
        
        
        
        public List<String[]> selectAllOrders()
        {
                List<String[]> list = new ArrayList<String[]>();
                Cursor cursor = db.query(TABLE_ORDERS, new String[] { "lineOrderId","clientName","productName","piecesNumber","discountNumber" }, null, null, null, null, "clientName asc"); 
                int x=0;
                if (cursor.moveToFirst()) {
                        do {
                                String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)};
                                list.add(b1);
                                x=x+1;
                        } while (cursor.moveToNext());
                }
                if (cursor != null && !cursor.isClosed()) {
                        cursor.close();
                } 
                cursor.close();
                return list;
        }


        public List<String[]> selectAllProducts()
        {

                List<String[]> list = new ArrayList<String[]>();
                Cursor cursor = db.query(TABLE_PRODUCTS, new String[] { "ID","Name","Price","Symbol" }, null, null, null, null, "Name asc"); 
                // (ID, Name, Price, Symbol)
                int x=0;
                if (cursor.moveToFirst()) {
                        do {
                                String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3)};
                                list.add(b1);
                                x=x+1;
                        } while (cursor.moveToNext());
                }
                if (cursor != null && !cursor.isClosed()) {
                        cursor.close();
                } 
                cursor.close();
                return list;
        }
        
        
        public List<String[]> selectAllClients()
        {

                List<String[]> list = new ArrayList<String[]>();
                Cursor cursor = db.query(TABLE_CLIENTS, new String[] { "Agent","Client","Route","Zone" }, null, null, null, null, "Client asc"); 
                int x=0;
                if (cursor.moveToFirst()) {
                        do {
                                String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3)};
                                list.add(b1);
                                x=x+1;
                        } while (cursor.moveToNext());
                }
                if (cursor != null && !cursor.isClosed()) {
                        cursor.close();
                } 
                cursor.close();
                return list;
        }
        

        public String[] selectFirstRecordFromSetupTable()
        {
                String[] setupInfo = null ;
                
                Cursor cursor = db.query(TABLE_SETUP, new String[] { "UtilizatorID","Parola","UserName","SefID", "ZonaID" }, null, null, null, null, "UserName asc");
                if (cursor.moveToFirst()) {
                	setupInfo = new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4)};
                }
                if (cursor != null && !cursor.isClosed()) {
                        cursor.close();
                } 
                cursor.close();
                return setupInfo;
        }

        public void delete(int rowId) {
                db.delete(TABLE_ORDERS, null, null);
        }

        // no-delete if missing ? 
        public void close() {
                db.close();
        }


        private static class OpenHelper extends SQLiteOpenHelper {

                OpenHelper(Context context) {
                        super(context, DATABASE_NAME, null, DATABASE_VERSION);
                }

                @Override
                public void onCreate(SQLiteDatabase db) {
                	    db.execSQL("CREATE TABLE " + TABLE_COMANDA + " (com_id integer primary key autoincrement, nrdoc character, data_c date, gestiune_id character, tert_id character, valoare numeric,nrlc_id character, data_l date, user_id character, nivacc integer, operare timestamp, verstor numeric, tiparit character, facturat character, zscadenta integer, pr_disc_expl numeric, val_disc_expl numeric, NrFact character, data_f date  )");
                        db.execSQL("CREATE TABLE " + TABLE_ORDERS +   " (lineOrderId INTEGER PRIMARY KEY, clientName TEXT, productName TEXT, piecesNumber TEXT, discountNumber TEXT)");
                        db.execSQL("CREATE TABLE " + TABLE_PRODUCTS + " (_id integer primary key autoincrement, ID TEXT, Name TEXT, Price TEXT, Symbol TEXT)");
                        db.execSQL("CREATE TABLE " + TABLE_CLIENTS + " (_id integer primary key autoincrement, Agent TEXT, Client TEXT, Route TEXT, Zone TEXT)");
                        db.execSQL("CREATE TABLE " + TABLE_SETUP + " (_id integer primary key autoincrement, UtilizatorID TEXT, Parola TEXT, UserName TEXT, SefID TEXT, ZonaID TEXT)");
                        // db.execSQL("INSERT INTO SETUP (UtilizatorID, UserName, Parola) values ('1','NOBODY','NOBODY')";
                        
                }

                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                	    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMANDA);
                        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDERS);
                        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
                        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CLIENTS);
                        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SETUP);
                        onCreate(db);
                }
        }
}

 */